package com.atguigu.guli.service.ucenter.service.impl;

import com.atguigu.guli.common.base.result.ResultCodeEnum;
import com.atguigu.guli.common.base.util.FormUtils;
import com.atguigu.guli.common.base.util.JwtInfo;
import com.atguigu.guli.common.base.util.JwtUtils;
import com.atguigu.guli.common.base.util.MD5;
import com.atguigu.guli.service.base.dto.MemberDto;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.vo.LoginVo;
import com.atguigu.guli.service.ucenter.entity.vo.RegisterVo;
import com.atguigu.guli.service.ucenter.mapper.MemberMapper;
import com.atguigu.guli.service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Helen
 * @since 2021-10-05
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void register(RegisterVo registerVo) {

        // 手机号
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        String formCode = registerVo.getCode();
        // 判断手机号是否合法
        if(StringUtils.isEmpty(mobile) || !FormUtils.isMobile(mobile)){
            throw new GuliException(ResultCodeEnum.LOGIN_MOBILE_ERROR);
        }

        if(StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(formCode)){
            throw new GuliException(ResultCodeEnum.PARAM_ERROR);
        }


        // 校验验证码
        String redisCode = (String)redisTemplate.opsForValue().get(mobile);

        if(!Objects.equals(redisCode,formCode)){
            throw new GuliException(ResultCodeEnum.CODE_ERROR);
        }

        // 判断用户是否被注册
        QueryWrapper<Member> memberWrapper = new QueryWrapper<>();
        // 用手机号或昵称 ，或者两个都判断
        memberWrapper.eq("mobile",mobile);
        Member memberData = baseMapper.selectOne(memberWrapper);

        if(!StringUtils.isEmpty(memberData)){
            throw new GuliException(ResultCodeEnum.REGISTER_MOBLE_ERROR);
        }

        // 注册，插入数据
        Member member = new Member();
        BeanUtils.copyProperties(registerVo,member);
        // 密码加密
        String encrypt = MD5.encrypt(member.getPassword());
        member.setPassword(encrypt);
        // 默认头像
        member.setAvatar("https://img.alicdn.com/imgextra/i3/O1CN01X5wCJo1y5PnqZmij7_!!6000000006527-1-tps-144-144.gif");
        // 是否禁用
        member.setDisabled(false);

        baseMapper.insert(member);
    }

    @Override
    public String login(LoginVo loginVo) {
        // 获取登录的 提交参数
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        // 校验参数是否合法
        if(StringUtils.isEmpty(mobile)
                ||!FormUtils.isMobile(mobile)
                || StringUtils.isEmpty(password)){
            throw new GuliException(ResultCodeEnum.PARAM_ERROR);
        }


        // 数据查询用户
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("mobile",mobile);
        Member one = this.getOne(memberQueryWrapper);
        if(StringUtils.isEmpty(one)){
            throw new GuliException(ResultCodeEnum.LOGIN_MOBILE_ERROR);
        }

        password = MD5.encrypt(password);
        String dataPwd = one.getPassword();
        // 校验密码是否正确
        if(!Objects.equals(dataPwd,password)){
            throw new GuliException(ResultCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        // 校验用户是否被禁用
        Boolean disabled = one.getDisabled();
        if(disabled){
            throw new GuliException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }

        // 登录：生成token
        JwtInfo jwtInfo = new JwtInfo();
        BeanUtils.copyProperties(one,jwtInfo);
        String jwtToken = JwtUtils.getJwtToken(jwtInfo, 60 * 30);// 有效时间为30分钟

        return jwtToken;
    }

    @Override
    public MemberDto getMemberDtoByMemberId(String memberId) {

        Member member = baseMapper.selectById(memberId);
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member,memberDto);
        return memberDto;
    }

    @Override
    public Integer CountRegisterNum(String day) {
        return baseMapper.selectRegisterNumByDay(day);
    }
}
